/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.panomedic.core;

import java.awt.Point;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

/**
 *
 * @author Yare
 */
public class ConnectedComponent {

    public static ArrayList<Point> filter(ArrayList<Point> inputList) {

        Queue<Point> queue = new LinkedList<Point>();

        ArrayList<Point> resultArray = new ArrayList<Point>();
//        resultArray = (ArrayList<Point>) inputList.clone();

        Iterator<Point> mainIt = inputList.iterator();

        HashSet<Point> odjebane = new HashSet<Point>();

        while (mainIt.hasNext()) {
            Point p = mainIt.next();
            if (odjebane.contains(p)) {
                continue;
            }
            odjebane.add(p);
            resultArray.add(p);

            // for all seek neighbour
            Iterator<Point> seekIt = inputList.iterator();
            while (seekIt.hasNext()) {
                Point p2 = seekIt.next();
                if (neighbours(p, p2)) {
                    queue.add(p2);
                }
            }
            while (!queue.isEmpty()) {
                Point p2 = queue.poll();
//                resultArray.remove(p2);
                odjebane.add(p2);

                Iterator<Point> seekIt2 = inputList.iterator();
                while (seekIt2.hasNext()) {
                    Point p3 = seekIt2.next();
                    if (neighbours(p2, p3) && !queue.contains(p3) && !odjebane.contains(p3)) {
                        queue.add(p3);
                    }
                }
            }
        }
        return resultArray;
    }

    protected static boolean neighbours(Point p, Point p2) {
        if (p.x == p2.x && p.y == p2.y - 1) {
            return true;
        } else if (p.x == p2.x && p.y == p2.y + 1) {
            return true;
        } else if (p.x == p2.x - 1 && p.y == p2.y) {
            return true;
        } else if (p.x == p2.x + 1 && p.y == p2.y) {
            return true;
        } else {
            return false;
        }
    }

//    public static void main(String[] args) {
//        Point p1 = new Point(5, 5);
//        Point p2 = new Point(5, 5);
//
//        System.out.println(neighbours(p1, p2));
//
//    }
    public static void main(String[] args) {

        ArrayList<Point> testAL = new ArrayList<Point>();

        testAL.add(new Point(3, 5));
        testAL.add(new Point(3, 2));
        testAL.add(new Point(1, 3));
        testAL.add(new Point(4, 5));
        testAL.add(new Point(1, 2));
        testAL.add(new Point(1, 4));
        testAL.add(new Point(2, 2));
        testAL.add(new Point(0, 5));
        testAL.add(new Point(2, 3));


        ArrayList<Point> testAL2 = new ArrayList<Point>();
        testAL2 = filter(testAL);

        System.out.println("1:");
        System.out.println(testAL);
        System.out.println("2:");
        System.out.println(testAL2);
    }
}
